<template>
  <div class="app-container">
    <el-row>
      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <el-col :span="1.5">
            <el-button v-permission="['mall:order:settings']" size="mini" plain type="primary" icon="el-icon-plus" @click="handleInfo()">
              新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-if="selectionData.length > 0" v-permission="['mall:order:settings']" size="mini" plain type="danger" icon="el-icon-delete" @click="handleDelete()">
              删除
            </el-button>
          </el-col>
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="id"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column align="center" label="秒杀订单超时时间(分)" prop="flashOrderOvertime" show-overflow-tooltip />
          <el-table-column align="center" label="正常订单超时时间(分)" prop="normalOrderOvertime" show-overflow-tooltip />
          <el-table-column align="center" label="自动确认收货时间(天)" prop="confirmOvertime" />
          <el-table-column align="center" label="自动完成交易时间(天)" prop="finishOvertime" />
          <el-table-column align="center" label="自动好评时间(天)" prop="commentOvertime" />
          <el-table-column align="center" label="所需会员成长值" prop="memberLevel" width="120">
            <template slot-scope="scope">
              <el-link :underline="false">
                {{ getDictValue('growth', scope.row.memberGrowth) }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column v-permission="['mall:order:settings']" align="center" label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <el-link class="m-r-10" icon="el-icon-view" @click="handleInfo(scope.row.id)">查看</el-link>
              <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <order-setting-dialog v-if="openFormDialog" ref="setting_form" @confirmForm="handleFilter" />
  </div>
</template>

<script>
import { memberLevelList } from '@/api/system/member'
import { orderSettingsTable, orderSettingsDelete } from '@/api/mall/order/settings'
import OrderSettingDialog from './form/dialog'

export default {
  components: {
    OrderSettingDialog
  },
  data() {
    return {
      showSearch: false,
      queryData: {},
      optionOfMemberGrowth: [],
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: [],
      openFormDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$nextTick(() => {
        this.getDictData()
      })
    },
    getDictData() {
      const that = this
      memberLevelList().then(response => {
        this.optionOfMemberGrowth = response
        that.getTableData()
      })
    },
    getDictValue(property, value) {
      if (property === 'growth') {
        let tempLevel = this.optionOfMemberGrowth[1]
        const index = this.optionOfMemberGrowth.findIndex(level => {
          if (level.growthPoint === value) {
            return true
          }
          if (value > level.growthPoint) {
            tempLevel = level
          }
          return false
        })
        if (index !== -1) {
          return this.optionOfMemberGrowth[index].name
        } else {
          return tempLevel.name
        }
      }
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      orderSettingsTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.queryTable.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.selectionData = val
    },
    handleInfo(id) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.setting_form.init(id)
      })
    },
    handleDelete(id) {
      const ids = this.selectionData.map(function(settings, index) {
        return settings.id
      }).join(',')

      id = id || ids
      if (!id || id.length === 1) {
        this.$modal.msgWarning('请先选中数据项')
        return
      }

      this.$confirm('是否删除所选设置?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        orderSettingsDelete({
          ids: id
        }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>
